//
//  SettlementDetailController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/12/4.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SettlementDetailController.h"
#import "SDetailView.h"
#import "SettlementCell.h"

@interface SettlementDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)SDetailView *detailV;
@property (nonatomic,weak)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *billData;
@property (nonatomic, assign) NSInteger PageNumber;
@property (nonatomic,strong)NSNumber *totalCountKey;

@end

@implementation SettlementDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"结算单明细";
    _billData  = [NSMutableArray array];
    [self setUI];
    [self loadData];
    [self loadReferView];
}
- (void)loadData{
    
    _PageNumber = 1;
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool Scdetailbill_id:_bill_id Page:_PageNumber pageSize:12 success:^(NSDictionary *dic) {

        NSDictionary *bill = [dic hw_safeObjectForKey:@"bill"];
        
        [weakSelf.detailV.SettNumberL hw_setAttributedText:[NSString stringWithFormat:@"结算单编号：<style font=14 color=#686868>%@</style>",bill[@"bill_sn"]]];
         [weakSelf.detailV.SetTimeL hw_setAttributedText:[NSString stringWithFormat:@"申请时间：<style font=14 color=#686868>%@</style>",[HWCommon getTimeStr:[bill hw_safeObjectForKey:@"bill_create_time"] format:@"yyyy-MM-dd HH:mm:ss"]]];
        if ([[bill hw_safeObjectForKey:@"bill_pay_time"] integerValue] == 0) {
            weakSelf.detailV.ApleTimeL.hidden = YES;
        }else{
            [weakSelf.detailV.ApleTimeL hw_setAttributedText:[NSString stringWithFormat:@"结算时间：<style font=14 color=#686868>%@</style>",[HWCommon getTimeStr:[bill hw_safeObjectForKey:@"bill_pay_time"] format:@"yyyy-MM-dd HH:mm:ss"]]];
        }
        [weakSelf.detailV.ApleTimeL hw_setAttributedText:[NSString stringWithFormat:@"申请时间：<style font=14 color=#686868>%@</style>",[HWCommon getTimeStr:[bill hw_safeObjectForKey:@"bill_create_time"] format:@"yyyy-MM-dd HH:mm:ss"]]];
        [weakSelf.detailV.SetPrcieL hw_setAttributedText:[NSString stringWithFormat:@"结算额：<style font=16 color=#fd6c40>%@</style>  核销单数：<style font=16 color=#5aa54d>%@</style>",bill[@"bill_result_total"],dic[@"count"]]];

        if ([bill[@"bill_state"] integerValue] == 1) {
            weakSelf.detailV.SettTypeL.text = @"申请中";
            weakSelf.detailV.SettTypeL.textColor =  WYColor(84, 156, 44);
            
            
        }else if ([bill[@"bill_state"] integerValue] == 2){
            weakSelf.detailV.SettTypeL.text = @"结算中";
            weakSelf.detailV.SettTypeL.textColor = KOrange;

        }else if ([bill[@"bill_state"] integerValue] == 3){
            weakSelf.detailV.SettTypeL.text = @"已结算";
            weakSelf.detailV.SettTypeL.textColor = WYColor(33, 185, 167);

        }
        
         weakSelf.totalCountKey = [dic hw_safeObjectForKey:@"count_page"];
         NSArray *billArr = [dic hw_safeObjectForKey:@"code_list"];
        weakSelf.billData = billArr.mutableCopy;
        [weakSelf performSelector:@selector(setBillData:) withObject:_billData];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSString *errormsg) {
        [SCTool showError:errormsg];
    }];
}
- (void)loadReferView
{
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)setBillData:(NSMutableArray *)billData{
    _billData = billData;
    if ([_billData count] < 12) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        [self.tableView.mj_footer setHidden:NO];
    }
}
- (void)loadMoreData{
    _PageNumber++;
    if ([_totalCountKey integerValue]< _PageNumber) {
        [SCTool showMessage:@"没有更多数据"];
        [_tableView.mj_footer endRefreshing];
        _PageNumber--;
        return ;
    }
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool Scdetailbill_id:_bill_id Page:_PageNumber pageSize:12 success:^(NSDictionary *dic) {
        [weakSelf.billData addObjectsFromArray:[dic hw_safeObjectForKey:@"code_list"]];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer endRefreshing];
        
    } failure:^(NSString *errormsg) {
        [SCTool showMessage:errormsg];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
- (void)setUI{
     SDetailView *detailV = [[[NSBundle mainBundle] loadNibNamed:@"SDetailView" owner:self options:nil] lastObject];
    [self.view addSubview:detailV];
    _detailV = detailV;
    __weak __typeof(&*self)weakSelf = self;
    [detailV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@118);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.view);
        make.top.equalTo(detailV.mas_bottom).offset(8);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.billData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettlementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettlementCell" forIndexPath:indexPath];
    cell.item = self.billData[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setLayoutMargins:)]){
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (UITableView *)tableView{
    if (_tableView == nil) {
        UITableView *tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        [tableV setSeparatorColor:DarkColor];
        tableV.rowHeight = 90;
        tableV.delegate = self;
        tableV.dataSource = self;
        [tableV registerNib:[UINib nibWithNibName:@"SettlementCell" bundle:nil] forCellReuseIdentifier:@"SettlementCell"];
        tableV.tableFooterView = [[UIView alloc] init];
        [self.view addSubview:tableV];
        _tableView = tableV;
    }
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
